<?php
// +----------------------------------------------------------------------
// | 基于Layui表格，支持所有表头属性，文档：https://layui.dev/docs/2/table/#options.cols
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace addons\BuilderPlus\components;

class Column
{

    public const RESERVE = [
        'lazyImageTpl' => 'builderLazyImageTpl',
        'lazyVideoTpl' => 'builderLazyVideoTpl',
        'fastSortTpl'  => '#builderFastSortTpl',
        'fastRatioTpl' => '#builderFastRatioTpl',
        'switchTpl'    => '#builderSwitchTpl',
        'friendlyTpl'  => '#builderFriendlyTpl'
    ];

    protected $config = [
        'field'        => '',
        'title'        => '',
        'width'        => '',
        'minWidth'     => '',
        'maxWidth'     => '',
        'type'         => 'normal',
        'sort'         => false,
        'withSort'     => '',
        'templet'      => '',
        'align'        => 'center',
        'fixed'        => '',
        'toolbar'      => '',
        'hide'         => false,
        'event'        => '',
        'style'        => '',
        'colspan'      => '',
        'rowspan'      => '',
        'fastSort'     => '',
        'fastRatio'    => '',
        'friendly'     => '',
        'switch'       => '',
        'editCol'      => '',
        'totalRow'     => false,
        'totalRowText' => '',
        'withImgTpl'   => [],
        'withVideoTpl' => []
    ];


    public function __construct(string $field, string $title)
    {
        $this->config['field'] = $field;
        $this->config['title'] = $title;
    }

    public function field(string $field)
    {
        $this->config['field'] = $field;
        return $this;
    }

    public function title(string $title)
    {
        $this->config['title'] = $title;
        return $this;
    }

    public function width(int|float $width)
    {
        $this->config['width'] = $width;
        return $this;
    }

    public function totalRow(string|bool $totalRow)
    {
        $this->config['totalRow'] = $totalRow;
        return $this;
    }

    public function totalRowText(string $totalRowText)
    {
        $this->config['totalRowText'] = $totalRowText;
        return $this;
    }

    public function minWidth(int|float $minWidth)
    {
        $this->config['minWidth'] = $minWidth;
        return $this;
    }

    public function maxWidth(int|float $maxWidth)
    {
        $this->config['maxWidth'] = $maxWidth;
        return $this;
    }

    public function type(string $type)
    {
        $this->config['type'] = $type;
        return $this;
    }

    public function sort()
    {
        $this->config['sort'] = true;
        return $this;
    }

    public function withSort(string $with)
    {
        $this->config['sort'] = true;
        $this->config['withSort'] = $with;
        return $this;
    }

    public function templet(string $templet)
    {
        $this->config['templet'] = $templet;
        return $this;
    }

    public function friendly()
    {
        $this->config['templet'] = self::RESERVE['friendlyTpl'] . '-' . $this->config['field'];
        $this->config['friendly'] = true;
        return $this;
    }

    public function align(string $align)
    {
        $this->config['align'] = $align;
        return $this;
    }

    public function fixed(string $fixed)
    {
        $this->config['fixed'] = $fixed;
        return $this;
    }

    public function toolbar(string $toolbar)
    {
        $this->config['toolbar'] = $toolbar;
        return $this;
    }

    public function hide()
    {
        $this->config['hide'] = true;
        return $this;
    }

    public function event(string $event)
    {
        $this->config['event'] = $event;
        return $this;
    }

    public function style(string $style)
    {
        $this->config['style'] = $style;
        return $this;
    }


    public function colspan(string $colspan)
    {
        $this->config['colspan'] = $colspan;
        return $this;
    }

    public function rowspan(string $rowspan)
    {
        $this->config['rowspan'] = $rowspan;
        return $this;
    }


    public function lazyImage()
    {
        $tplId = self::RESERVE['lazyImageTpl'] . '_' . $this->config['field'];
        $this->config['templet'] = '#' . $tplId;
        $this->config['withImgTpl'] = [
            'field' => $this->config['field'],
            'tplId' => $tplId
        ];
        return $this;
    }

    public function lazyVideo()
    {
        $tplId = self::RESERVE['lazyVideoTpl'] . '_' . $this->config['field'];
        $this->config['templet'] = '#' . $tplId;
        $this->config['withVideoTpl'] = [
            'field' => $this->config['field'],
            'tplId' => $tplId
        ];
        return $this;
    }

    public function fastRatio()
    {
        $this->config['templet'] = self::RESERVE['fastRatioTpl'] . '-' . $this->config['field'];
        $this->config['fastRatio'] = true;
        return $this;
    }

    public function fastSort(string $url = '')
    {
        $this->config['templet'] = self::RESERVE['fastSortTpl'] . '-' . $this->config['field'];
        $this->config['fastSort'] = $url ?: url('update')->build();
        $this->config['width'] = 100;
        return $this;
    }

    public function switch(string $text, string $val, string $url = '')
    {
        $this->config['templet'] = self::RESERVE['switchTpl'] . '-' . $this->config['field'];;
        $this->config['width'] = 100;
        $this->config['switch'] = [
            'text'    => $text,
            'val'     => $val,
            'checked' => explode('|', $val)[0],
            'url'     => $url ?: url('update')->build()
        ];
        return $this;
    }

    public function getFastSort()
    {
        return $this->config['fastSort'];
    }

    public function editCol($editCol = [])
    {
        return $this->config['editCol'] = $editCol;
    }

    public function getSwitch()
    {
        return $this->config['switch'];
    }

    public function getWithImgTpl()
    {
        return $this->config['withImgTpl'];
    }

    public function render()
    {
        return $this->config;
    }

    public function __call($name, $arguments)
    {
        if (isset($this->config[$name])) {
            $this->config[$name] = $arguments;
        }
        return $this;
    }
}